package convert;

import java.util.ArrayList;
import java.util.HashMap;

import main.ErrorDefine;
import main.ErrorException;

public class BaseContent 
{
	protected String m_name;
	protected ArrayList<BaseContent> m_children;
	protected HashMap<String, ArrayList<BaseContent>> m_map;
	protected BaseContent m_father;
	
	public BaseContent(String name, BaseContent father)
	{
		m_name = name;
		m_father = father;
	}
	
	public BaseContent getFather()
	{
		return m_father;
	}
	
	public String getName()
	{
		return m_name;
	}
	
	public void addChild(BaseContent bc)
	{
		if(m_children == null)
		{
			m_map = new HashMap<>();
			m_children = new ArrayList<>();
		}
		m_children.add(bc);
		ArrayList<BaseContent> list = null;
		if(!m_map.containsKey(bc.getName()))
		{
			list = new ArrayList<>();
			m_map.put(bc.getName(), list);
		}
		else
		{
			list = m_map.get(bc.getName());
		}
		list.add(bc);
	}
	
	public int getChildNumber()
	{
		if(m_children == null)
		{
			return 0;
		}
		return m_children.size();
	}

	public BaseContent getChild(int index) throws ErrorException
	{
		if(m_children == null || index < 0 || index >= m_children.size())
		{
			throw new ErrorException(ErrorDefine.E_CHILD_INDEX_WRONG_ERR, index + "");
		}
		return m_children.get(index);
	}
	
	public ArrayList<BaseContent> getChildrenByName(String name)
	{
		return m_map.get(name);
	}
	
	public BaseContent getChild(String name) throws ErrorException
	{
		return getChild(name, 0);
	}
	
	public BaseContent getChild(String name, int index) throws ErrorException
	{
		ArrayList<BaseContent> list = m_map.get(name);
		if(list == null)
		{
			throw new ErrorException(ErrorDefine.E_CHILD_NAME_WRONG_ERR, name);
		}
		if(index < 0 || index >= list.size())
		{
			throw new ErrorException(ErrorDefine.E_CHILD_NAME_INDEX_WRONG_ERR, name + "," + index);
		}
		return list.get(index);
	}
	
	public BaseContent getChildWithoutError(int index)
	{
		if(m_children != null && index >= 0 && index < m_children.size())
		{
			return m_children.get(index);
		}
		return null;
	}
	
	public BaseContent getChildWithoutError(String name, int index)
	{
		ArrayList<BaseContent> list = m_map.get(name);
		if(list != null)
		{
			if(index >= 0 && index < list.size())
			{
				return list.get(index);
			}
		}
		return null;
	}
	
	public BaseContent hasChild(int index)
	{
		if(m_children == null || m_children.size() <= index || index < 0)
		{
			return null;
		}
		return m_children.get(index);
	}
	
	public BaseContent hasChild(String name)
	{
		return hasChild(name, 0);
	}
	
	public BaseContent hasChild(String name, int index)
	{
		if(m_map == null)
		{
			return null;
		}
		ArrayList<BaseContent> list = m_map.get(name);
		if(list == null)
		{
			return null;
		}
		if(index < 0 || index >= list.size())
		{
			return null;
		}
		return list.get(index);
	}
	
	public void removeChild(BaseContent con)
	{
		if(m_children != null)
		{
			m_children.remove(con);
			ArrayList<BaseContent> list = m_map.get(con.getName());
			if(list != null)
			{
				list.remove(con);
			}
		}
	}
}
